/*******************************************************************************
 * 
 ******************************************************************************/
var searchPriceWin;
var commodityGrid;
var providerGrid;
var c_l_store;
var c_s_store;
var c_cm;
var p_l_store;
var p_s_store;
var p_cm;
var current_selected_id;
var current_selected_name;
var p_sel_ids = [];

var pageSize = 5;

Ext.onReady(function() {
	/*
	 * Ext.QuickTips.init();
	 * 
	 * Ext.form.Field.prototype.msgTarget = "side";
	 * 
	 * Ext.BLANK_IMAGE_URL =
	 * "http://localhost/extjstest/resources/images/default/s.gif";
	 */

	TopicRecord = Ext.data.Record.create([{
				name : 'id',
				mapping : 'commodity.id'
			}, {
				name : 'commodity_name',
				mapping : 'commodity.commodity_name'
			}, {
				name : 'category_name',
				mapping : 'commodity.category.category_name'
			}, {
				name : 'current_price',
				mapping : 'commodity.current_price_old'
			}]);

	c_cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			new Ext.grid.CheckboxSelectionModel(), {

				header : "编号",

				align : "center",

				dataIndex : "id"

			}, {

				header : "商品名",

				align : "center",

				dataIndex : "commodity_name"

			}, {

				header : "商品类型",

				dataIndex : "category_name",

				align : "center"

			}, {

				header : "商品价格",

				dataIndex : "current_price",

				align : "center"

			}]);

	c_l_store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : "commodityManagementAction!list.action"
						}),
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'commodity_list',
							successProperty : 'SUCCESS',
							fields : [{
										name : 'id',
										mapping : 'id'
									}, {
										name : 'commodity_name',
										mapping : 'commodity_name'
									}, {
										name : 'category_name',
										mapping : 'category.category_name'
									}, {
										name : 'current_price',
										mapping : 'current_price',
										type : 'float'
									}]
						}),

				sortInfo : {
					field : 'id',
					direction : 'ASC'
				}

			});

	c_s_store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : "commodityManagementAction!search.action"
						}),
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'commodity_list',
							successProperty : 'SUCCESS',
							fields : [{
										name : 'id',
										mapping : 'id'
									}, {
										name : 'commodity_name',
										mapping : 'commodity_name'
									}, {
										name : 'category_name',
										mapping : 'category.category_name'
									}, {
										name : 'current_price',
										mapping : 'current_price',
										type : 'float'
									}]
						}),

				sortInfo : {
					field : 'id',
					direction : 'ASC'
				}

			});
			
	p_cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			new Ext.grid.CheckboxSelectionModel(), {

				header : "编号",

				align : "center",

				dataIndex : "id"

			}, {

				header : "供应商名称",

				align : "center",

				dataIndex : "provider_name"

			}, {

				header : "地址",

				dataIndex : "address",

				align : "center"

			}, {

				header : "电话",

				dataIndex : "tel",

				align : "center"

			}]);		
			
	p_l_store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : "commodityManagementAction!listProviders.action"
						}),
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'provider_list',
							successProperty : 'SUCCESS',
							fields : [{
										name : 'id',
										mapping : 'id'
									}, {
										name : 'provider_name',
										mapping : 'provider_name'
									}, {
										name : 'address',
										mapping : 'address'
									}, {
										name : 'tel',
										mapping : 'tel'
									}]
						}),

				sortInfo : {
					field : 'id',
					direction : 'ASC'
				}

			});

	p_s_store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : "commodityManagementAction!searchProviders.action"
						}),
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'provider_list',
							successProperty : 'SUCCESS',
							fields : [{
										name : 'id',
										mapping : 'id'
									}, {
										name : 'provider_name',
										mapping : 'provider_name'
									}, {
										name : 'address',
										mapping : 'address'
									}, {
										name : 'tel',
										mapping : 'tel'
									}]
						}),

				sortInfo : {
					field : 'id',
					direction : 'ASC'
				}

			});		
			
			
	searchCommodityWin = new Ext.Window({
				title : '查找商品',
				layout : 'form',
				width : 300,
				height : 180,
				closeAction : 'hide',
				buttonAlign : 'center',
				labelWidth : 55,
				modal : true,
				plain : true,

				buttons : [{
					text : '查找',
					handler : function() {
						commodityGrid.getStore().removeAll();
						commodityGrid.reconfigure(c_s_store, c_cm);
						//alert("name:" + Ext.getCmp("searchName").getValue());
						c_s_store.load({
									params : {
										start : 0,
										limit : pageSize,
										"commodity.id" : Ext.getCmp("searchId")
												.getValue(),
										"commodity.commodity_name" : Ext.getCmp("searchName")
												.getValue()
									}
								});
						searchCommodityWin.hide();
						Ext.getCmp("searchId").setValue("");
						Ext.getCmp("searchName").setValue("");
					}

				}, {
					text : '取消',
					handler : function() {
						Ext.getCmp("searchId").setValue("");
						Ext.getCmp("searchName").setValue("");
						searchCommodityWin.hide();
					}
				}],
				items : [{
							fieldLabel : '商品编号',
							width : 200,
							id : 'searchId',
							name : 'searchId',
							// allowBlank : false,
							xtype : 'textfield'

						}, {

							fieldLabel : '商品名称',
							id : 'searchName',
							name : 'searchName',
							width : 200,
							// allowBlank : false,
							xtype : 'textfield'

						}]

			});

	commodityGrid = new Ext.grid.GridPanel({
		renderTo : 'gridPanel',
		store : c_l_store,
		cm : c_cm,
		autoHeight : true,
		title : '商品管理',
		frame : true,
		stripeRows : true,
		tbar : [{
					text : "查询商品",
					handler : function() {
						searchCommodityWin.show();
					},
					scope : this
				}, "-", {
					text : "供应商列表",
					handler : function() {
						var sm = commodityGrid.getSelectionModel();
						if (sm.hasSelection()) {
							var record = sm.getSelected();
							current_selected_id = record.get("id");
							current_selected_name = record.get("commodity_name");
							//alert(':' + current_selected_name);
							commodityGrid.hide();
							providerGrid.setTitle("商品："+current_selected_name+" 的供应商列表");
							providerGrid.show();
							p_s_store.load({
										params : {
											start : 0,
											limit : pageSize,
											current_selected_id : current_selected_id
										}
									});
						} else {
							Ext.Msg.alert('信息', "请选中一行！");
						}
					},
					scope : this
				}],
		enableColumnMove : false,
		selModel : new Ext.grid.CheckboxSelectionModel(),
		bbar : new Ext.PagingToolbar({
					pageSize : pageSize,
					store : c_l_store
				})
	});
	
	providerGrid = new Ext.grid.GridPanel({
		renderTo : 'gridPanel',
		title : "供应商列表",
		store : p_s_store,
		cm : p_cm,
		autoHeight : true,
		frame : true,
		stripeRows : true,
		tbar : [{
			text : "添加供应商",
			handler : function() {
				addProvidersWin.show();
				addProvidersGrid.setTitle("为商品" + current_selected_name
						+ "添加供应商");
				addProvidersGrid.show();
				addProvidersGrid.getStore().load({
							params : {
								start : 0,
								limit : pageSize
							}
						});
				addProvidersWin.center();
			},
			scope : this
		}, "-", {
			text : "删除供应商",
			handler : function() {
				var sm = providerGrid.getSelectionModel();
				if (sm.hasSelection()) {
					Ext.Msg.confirm('信息', '确定要删除?', function(btn) {
						if (btn == 'yes') {
							var _sr = providerGrid.getSelectionModel()
									.getSelections();
							p_sel_ids = [];
							for (var i = 0; i < _sr.length; i++) {
								p_sel_ids[i] = _sr[i].get("id");
							}
							Ext.Ajax.request({
										url : "commodityManagementAction!deleteProviders.action",
										params : {
											selectedProviderList : p_sel_ids,
											current_selected_id : current_selected_id
										}
									});

							/*
							 * for (var i = 0; i < _sr.length; i++) { //
							 * alert(_sr[i].get("id")); resourceGrid .getStore()
							 * .remove(resourceGrid .getStore()
							 * .getAt(_sr[i].get("id"))); }
							 */

							providerGrid.getStore().removeAll();
							providerGrid.getStore().reload({
										params : {
											start : 0,
											limit : pageSize,
											current_selected_id : current_selected_id
										}
									});
							providerGrid.getStore().removeAll();
							providerGrid.getStore().reload({
										params : {
											start : 0,
											limit : pageSize,
											current_selected_id : current_selected_id
										}
									});
						} else {
							Ext.Msg.alert('信息', "请选中一行！");
						}
					});
				} else {
					Ext.Msg.alert('信息', "请选中纪录！");
				}
			},
			scope : this
		}, "-", {
			text : "商品列表",
			handler : function() {
				providerGrid.hide();
				commodityGrid.show();
			},
			scope : this
		}],
		enableColumnMove : false,
		selModel : new Ext.grid.CheckboxSelectionModel(),
		bbar : new Ext.PagingToolbar({
					pageSize : pageSize,
					store : p_s_store
				})
	});

	addProvidersGrid = new Ext.grid.GridPanel({
		store : p_l_store,
		cm : p_cm,
		title : "添加供应商",
		autoHeight : true,
		frame : true,
		stripeRows : true,
		tbar : [{
			text : "添加供应商",
			handler : function() {
				var sm = addProvidersGrid.getSelectionModel();
				if (sm.hasSelection()) {
					Ext.Msg.confirm('信息', '确定要添加?', function(btn) {
						if (btn == 'yes') {
							var _sr = addProvidersGrid.getSelectionModel()
									.getSelections();
							p_sel_ids = [];
							for (var i = 0; i < _sr.length; i++) {
								p_sel_ids[i] = _sr[i].get("id");
							}
							alert("current_selected_id:" + current_selected_id);
							Ext.Ajax.request({
										url : "commodityManagementAction!addProviders.action",
										params : {
											selectedProviderList : p_sel_ids,
											current_selected_id : current_selected_id
										}
									});

							addProvidersWin.hide();
							providerGrid.getStore().removeAll();
							providerGrid.getStore().reload({
										params : {
											start : 0,
											limit : pageSize,
											current_selected_id : current_selected_id
										}
									});
						} else {
							Ext.Msg.alert('信息', "请选中一行！");
						}
					});
				} else {
					Ext.Msg.alert('信息', "请选中纪录！");
				}
			},
			scope : this
		}],
		enableColumnMove : false,
		selModel : new Ext.grid.CheckboxSelectionModel(),
		bbar : new Ext.PagingToolbar({
					pageSize : pageSize,
					store : p_l_store
				})
	});

	addProvidersWin = new Ext.Window({
				layout : 'fit',
				width : 330,
				height : 180,
				closeAction : 'hide',
				modal : true,
				plain : true,
				autoHeight : true,
				autoWidth : true,
				items : [addProvidersGrid]
			});

	p_s_store.on('beforeload', function() {
								Ext.apply(this.baseParams, {
									current_selected_id : current_selected_id
								});
							});	
	
	commodityGrid.show();
	providerGrid.hide();
	addProvidersGrid.hide();
	commodityGrid.getStore().load({
				params : {
					start : 0,
					limit : pageSize
				}
			});
			
});
